package com.arron.algorithm.leetcodetop100.链表;

import com.arron.algorithm.leetcodetop100.ListNode;

public class K个一组反转链表 {



    public ListNode reverseKGroup(ListNode head, int k) {

        ListNode p1 = head;
        for (int i = 0; i < k; i++) {
            if ( p1 == null){
                return head;
            }
            p1 = p1.next;
        }
        ListNode node = reverseNode(head, p1);

        head.next =  reverseKGroup(p1,k);

        return node;
    }

    public ListNode reverseNode(ListNode a,ListNode b){


        ListNode cur = a;
        ListNode pre = null;
        ListNode next= null;

        while (cur !=b){
            next =cur.next;
            cur.next = pre;
            pre = cur;
            cur= next;
        }


        return pre;
    }



}




